package com.example.fangqidian.fragment.index.bookcase;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.fangqidian.R;
import com.example.fangqidian.adapter.BaseRecycleViewAdapter;
import com.example.fangqidian.fragment.BaseFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期:2019/1/18 on 14:34
 * 描述：
 * 作者: zhougan
 */
public class BookcaseFragment1 extends BaseFragment {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private ArrayList<Book> books;
    private RecyclerViewAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(savedInstanceState!=null){
          books = savedInstanceState.getParcelableArrayList("books");
        }

        return inflater.inflate(R.layout.bookcase_pager1,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mSwipeRefreshLayout=view.findViewById(R.id.swipeRefreshLayout);
        mRecyclerView=view.findViewById(R.id.recycleView);
        init();
    }
    private void init(){
        initBooks();
        initRecyclerView();
        initSwipeRefreshLayout();
    }

    private void initSwipeRefreshLayout(){
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                flush();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }
    private void initBooks(){
        if(books==null){
            books=new ArrayList<>();
            for(int i=0;i<30;i++){
                books.add(new Book("书籍名字"+i,"作者"+i));
            }
        }

    }

    private int num=30;
    private void flush(){
        List<Book> list=new ArrayList<>();
        int end=num+30;
        while (num<end){
            list.add(new Book("书籍名字"+num,"作者"+num));
            num++;
        }
        books.clear();
        books.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    private void initRecyclerView(){
        mAdapter=new RecyclerViewAdapter();
        mAdapter.setData(books);


        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), OrientationHelper.VERTICAL,false));
        mRecyclerView.setAdapter(mAdapter);
    }




    private class RecyclerViewAdapter extends BaseRecycleViewAdapter<Book>{

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.bookcase_page1_list_item;
        }

        @Override
        public void onBindData(VH holder, Book data, int position, int viewType) {
            holder.setText(R.id.name,data.name);
            holder.setText(R.id.author,data.author);

        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putParcelableArrayList("books",books);


    }

    private static class Book implements Parcelable {
        private String name;
        private String author;

        public Book(String name, String author) {
            this.name = name;
            this.author = author;
        }

        protected Book(Parcel in) {
            name = in.readString();
            author = in.readString();
        }

        public static final Creator<Book> CREATOR = new Creator<Book>() {
            @Override
            public Book createFromParcel(Parcel in) {
                return new Book(in);
            }

            @Override
            public Book[] newArray(int size) {
                return new Book[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeString(author);
        }
    }

}
